/**
 * phi
 */



// ----------------------------------------------------------------
// param
// ----------------------------------------------------------------
var FPS = 30;
var FONT_FAMILY = "'メイリオ'";
var COLUMN      = 16;
var ROW         = 16;
var FONT_SIZE   = 16;
var FONT_WIDTH  = 32;
var FONT_HEIGHT = 32;
var GRID        = false;
var BOLD        = false;
var ITALIC      = false;

// ----------------------------------------------------------------
// global
// ----------------------------------------------------------------
var global = {
    canvas: null,
    keyboard: [],
    
    root: null,
    bullet: null,
    particles: []
};



var FontSprite = null;

TM.loadScript("dom", "dom");
TM.loadScript("dom", "canvas");
TM.loadScript("app");

// 
TM.addNamespace("dat-gui", "../../plugins/dat-gui");
TM.loadDatGUI();

// 
TM.addNamespace("stats", "../../plugins/Stats");
TM.loadStats();

var defineFontSprite = function(){
    
    var createFontTexture = function()
    {
        var SIZE = FONT_SIZE;
        
        var fontTexture = TM.App.Texture();
        fontTexture.canvas.width = 16*SIZE;
        fontTexture.canvas.height= 16*SIZE;
        fontTexture.canvas.width = COLUMN* FONT_WIDTH;
        fontTexture.canvas.height= ROW   * FONT_HEIGHT;
        
        fontTexture.context.font = FONT_SIZE + "px" + " " + "'ＭＳ Ｐゴシック'";
        fontTexture.context.font = FONT_SIZE + "px" + " " + FONT_FAMILY;
        if (BOLD)   fontTexture.context.font = "bold " + fontTexture.context.font;
        if (ITALIC) fontTexture.context.font = "italic " + fontTexture.context.font;
        fontTexture.context.textAlign = "center";
        fontTexture.context.textBaseline = "middle";
        
        /*
        fontTexture.context.shadowBlur = 5;
        fontTexture.context.shadowColor = "#000";
        fontTexture.context.shadowOffsetX = 5;
        */
        
        var offset = 12353;
        
        for (var i=0; i<ROW; ++i) {
            for (var j=0; j<COLUMN; ++j) {
                var ch = String.fromCharCode(offset+i*COLUMN+j);
                if (global.ta) {
                    ch = global.ta.value[i*COLUMN+j];
                    ch = ch || ' ';
                }
                fontTexture.context.fillText(ch, j*FONT_WIDTH+FONT_WIDTH/2, i*FONT_HEIGHT+FONT_HEIGHT/2, FONT_SIZE);
                if (GRID) {
                    fontTexture.context.strokeRect(j*FONT_WIDTH, i*FONT_HEIGHT, FONT_WIDTH, FONT_HEIGHT);
                }
            }
        }
        
        return fontTexture;
    };
    
    
    FontSprite = TM.createClass({
        
        superClass: TM.App.Sprite,
        
        init: function(x, y){
            TM.App.Sprite.prototype.init.call(this, createFontTexture().canvas);
            
            this.x = window.innerWidth/2;
            this.y = window.innerHeight/2;
            
            if (this.graphics.width > 512) {
                this.scaleX = 512/this.width;
                this.scaleY = 512/this.height;
            }
        }
        
    });
    
};


var app = null;

/**
 * main
 */
TM.main(function()
{
    init();
    run();
    
    // テキストエリア生成
    var ta = global.ta = TM.$DOMElement(TM.$create("textarea"));
    ta.value = "\n!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~ ¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ";
    ta.fixed(20, 100);
    TM.$DOMElement(document.body).append(ta);
    
    window.onresize = reload;
    
});

var init = function()
{
    // クラスを定義
    defineFontSprite();
    
    // create app
    app = TM.App.CanvasApp();
    app.fps = 30;
    app.scene.background = "rgba(255, 255, 255, 1)";
    global.fontSprite = app.scene.addChild(FontSprite());
    // app.canvas.element.style.position = "relative";
    // app.canvas.element.style.width  = window.innerWidth;
    // app.canvas.element.style.height = window.innerHeight;
    
    app.enableStats();
    
    if (window.dat) {
        var gui = new dat.GUI();

        gui.add(window, "fontCreate");
        
        gui.add(window, "FONT_FAMILY").onChange(reload);
        gui.add(window, "COLUMN", 1, 64, 1).onChange(reload);
        gui.add(window, "ROW", 1, 64, 1).onChange(reload);
        
        gui.add(window, "FONT_SIZE", 4, 128, 1).onChange(reload);
        gui.add(window, "FONT_WIDTH", 4, 128, 1).onChange(reload);
        gui.add(window, "FONT_HEIGHT", 4, 128, 1).onChange(reload);
        
        gui.add(window, "GRID", true).onChange(reload);
        gui.add(window, "BOLD", true).onChange(reload);
        gui.add(window, "ITALIC", true).onChange(reload);
    }
};

var reload = function()
{
    app.scene.removeChildren();
    global.fontSprite = app.scene.addChild(FontSprite());
    // app.canvas.element.style.position = "relative";
    // app.canvas.element.style.width  = window.outerWidth;
    // app.canvas.element.style.height = window.outerHeight;
};

var run = function()
{
    app.run();
};

var fontCreate = function()
{
    global.fontSprite.graphics.saveAsImage();
};

























